import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.*;

/**
 * I represent a timer that counts backwards
 * @author Hugo Armando Rodríguez Flores
 * @version May 13th 2013
 */
public class Timer extends Actor
{
    private long startTime; //This takes the start time from the system
    private long elapsedTime;   //This takes the elapsed time since the beggining of the stage
    private long elapsedSecs;   //This are seconds calculated from elapsed time
    private long timerMax;      //The maximum time has been asigned for every stage
    private boolean startFlag;  //True when the user started playing
    
    /**
     * Sets the properties for the Timer
     * @param mins is the maximum time to be set on the timer.
     */
    public Timer(int mins)
    {
        timerMax=mins*60;
        elapsedTime=elapsedSecs=0;
        startFlag=true;
        this.setImage(new GreenfootImage(""+timerMax, 25, Color.WHITE, Color.BLACK));
    }
    
    /**
     * This makes the Timer count backwards
     */
    public void act() 
    {
        if (startFlag)
        {
            startTime=System.currentTimeMillis();
            startFlag=false;
        }
        elapsedTime=System.currentTimeMillis()-startTime;
        elapsedSecs=elapsedTime/1000;
        this.setImage(new GreenfootImage(""+(timerMax-elapsedSecs), 25, Color.WHITE, Color.BLACK));
    }
    
    /**
     * @return the elapsed time in game
     */
    public long getElapsedTime()
    {
        return timerMax-elapsedSecs;
    }
}
